package com.geek.concurrence4work;

import lombok.Data;

import java.util.concurrent.TimeUnit;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description: InheriatbleThreadLocal传递变量
 */

public class InheriatbleThreadLocalMethod {
    private static ThreadLocal threadLocal = new InheritableThreadLocal();

    @Data
    private static class Result {
        private Long i = 1L;
    }

    private static class MyThread extends Thread {

        @Override
        public void run() {
            Result res = (Result) threadLocal.get();
            res.setI(Homework03.sum3());
            threadLocal.set(res);
        }

    }

    public static void main(String[] args) throws InterruptedException {
        Result result = new Result();
        threadLocal.set(result);
        new MyThread().start();
        TimeUnit.SECONDS.sleep(1);
        Result res = (Result) threadLocal.get();
        System.out.println(res.getI());
    }


}
